package com.itheima.app.videomanager;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.itheima.domain.mongo.FocusUser;
import com.itheima.domain.mongo.RecommendVideo;
import com.itheima.domain.mongo.Video;
import com.itheima.service.UserInfoService;
import com.itheima.service.mongo.FocusUserService;
import com.itheima.service.mongo.VideoService;
import com.itheima.util.ConstantUtil;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.VideoVo;
import com.itheima.vo.VisitorVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class VideoManager {
    @DubboReference
    private VideoService videoService;
    @DubboReference
    private UserInfoService userInfoService;
    //查询推荐视频列表
    public PageBeanVo findVideoVoByPage(Long userId, Integer pageNum, Integer pageSize) {
        PageBeanVo videoByPage = videoService.findVideoByPage(userId, pageNum, pageSize);
        //创建结果返回集合
        ArrayList<VideoVo> videoVos = new ArrayList<>();
        List<Video> VideoList = (List<Video>) videoByPage.getItems();
        //判断是否为空
        if (CollectionUtil.isNotEmpty(VideoList)) {

            for (Video video : VideoList) {
                VideoVo videoVo = new VideoVo();
                //获取视屏用户信息
                videoVo.setUserInfo(userInfoService.findUserInfoById(video.getUserId()));
                videoVo.setVideo(video);
                videoVos.add(videoVo);
            }
        }
        videoByPage.setItems(videoVos);
        return videoByPage;
    }
    @DubboReference
    private FocusUserService focusUserService;
    @Autowired
    private StringRedisTemplate redisTemplate;
   //关注用户
    public void guanzhu(Long userId, Long focusUserId) {
        //添加道数据库中
        FocusUser focusUser = new FocusUser();
        focusUser.setCreated(System.currentTimeMillis());
        focusUser.setUserId(userId);
        focusUser.setFocusUserId(focusUserId);
        focusUserService.saveFocusUser(focusUser);
        //存入redis中
        redisTemplate.opsForValue().set(StrUtil.format(ConstantUtil.FOCUS_USER,userId,focusUserId),"1");


    }
    //取消关注
    public void removeguanzhu(Long userId, Long focusUserId) {
        focusUserService.removeFocusUser(userId,focusUserId);
        redisTemplate.delete(StrUtil.format(ConstantUtil.FOCUS_USER,userId,focusUserId));
    }
}
